﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Windows;

namespace SpriteExtractor.ViewModel
{
    class MainWindowViewModel : ViewModelBase
    {
  
        public Model.ImageManager Extractor { get; protected set; }

        public ImageViewModel Image { get { return new ImageViewModel(Extractor.Image); } }
        public IFileSystemService FileSystemService { get; set; }

        public MainWindowViewModel(Model.ImageManager extractor)
        {
            Extractor = extractor;
        }

        #region Commands

        public List<CommandViewModel> ImageCommands
        {
            get
            {
                return new List<CommandViewModel> 
                {
                    new CommandViewModel("_Open Image...", new RelayCommand(param=>this.OpenImage())), 
                    new CommandViewModel("_Load Sprites...", new RelayCommand(param=>this.LoadSprites())),
                    new CommandViewModel("_Save Sprites...", new RelayCommand(param=>this.SaveSprites(), param => !String.IsNullOrEmpty(Image.Path))), 
                    new CommandViewModel("_Export Sprites...", new RelayCommand(param=>this.ExportSprites(), param => !String.IsNullOrEmpty(Image.Path))), 
                    new CommandViewModel(Image.AddSpriteCommand.DisplayName, new RelayCommand(param=>this.AddSprite(), param => !String.IsNullOrEmpty(Image.Path))), 
                };
            }
        }

        private void SaveSprites()
        {
            string path = FileSystemService.BrowseFileToSave(
                new KeyValuePair<string, string>[]{
                    new KeyValuePair<string, string>("XML files", "*.xml"), 
                    new KeyValuePair<string, string>("All files", "*.*")
                });

            if (path != null)
            {
                try
                {
                    Extractor.SaveSprites(path);
                }
                catch (Exception e)
                {
                    PrintError(e);
                }
            }
        }

        private void ExportSprites()
        {
            string path = FileSystemService.BrowseFileToSave(
                new KeyValuePair<string, string>[]{
                    new KeyValuePair<string, string>("XML files", "*.xml"), 
                    new KeyValuePair<string, string>("All files", "*.*")
                });

            if (path != null)
            {
                try
                {
                    Extractor.ExportSprites(path);
                }
                catch (Exception e)
                {
                    PrintError(e);
                }
            }
        }

        private void LoadSprites()
        {
            string path = FileSystemService.BrowseFileToOpen(
                new KeyValuePair<string, string>[]{
                    new KeyValuePair<string, string>("XML files", "*.xml"), 
                    new KeyValuePair<string, string>("All files", "*.*")
                });

            if (path != null)
            {
                try
                {

                    Extractor.LoadSprites(path);
                    OnPropertyChanged("Image");
                }
                catch (Exception e)
                {
                    PrintError(e);
                }
            }
        }

        
        private void OpenImage()
        {
            Debug.Assert(FileSystemService != null);
            string path = FileSystemService.BrowseFileToOpen(
                new KeyValuePair<string, string>[]{
                    new KeyValuePair<string, string>("PNG Images", "*.png"), 
                    new KeyValuePair<string, string>("JPEG Images", "*.jpg"), 
                    new KeyValuePair<string, string>("All files", "*.*")
                });

            if (path != null)
            {
                try
                {
                    Extractor.Image = new Model.Image(path);
                    OnPropertyChanged("Image");
                }
                catch (Exception e)
                {
                    PrintError(e);
                }
            }
        }

        private void AddSprite()
        {
            Image.AddSpriteCommand.Command.Execute(null);
            OnPropertyChanged("Image");
        }

        #endregion

        private void PrintError(Exception e)
        {
            Debug.Print(e.StackTrace);
            MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            //TODO show errors in a status bar or create a special service
        }
    }
}
